package com.wanghua.spring.filter.response;

import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

/*
* 当 Servlet 中使用该 response 对象调用 getWriter() 来输出内容时，内容将被输出到 CharArrayWriter 对象中，达到缓存的效果（并没有正真输出到客户端）。
* 注意：如果 response 输出的内容为字符类内容，则会调用 getWriter()；如果为二进制内容，如图像数据等，则会调用 getOutputStream()。
* */
public class HttpCharacterResponseWrapper extends HttpServletResponseWrapper {
    private CharArrayWriter charArrayWriter = new CharArrayWriter();

    public HttpCharacterResponseWrapper(HttpServletResponse response) {
        super(response);
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        return new PrintWriter(charArrayWriter);
    }

    public CharArrayWriter getCharArrayWriter() {
        return charArrayWriter;
    }
}
